package com.seeway.wms.utils.ext


import android.app.Activity
import android.app.Service
import android.os.Vibrator
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import com.geen.tuoke.MyApplication
import com.geen.tuoke.R
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


//错误提示框
private var tipDialogBuilder: QMUITipDialog? = null

interface TipCallBack {

    fun dismiss()
}

interface OptionCallBack {
    fun optionIndex(index: Int)
}

/***
 * 弹出选择框
 */
fun AppCompatActivity.showOptionDialog(
    listItems: ArrayList<String>,
    callBack: OptionCallBack? = null,
    cancelAble:Boolean = true,
    title:String = ""
) {
    if (listItems.isNullOrEmpty()) {
        return
    }
    QMUIDialog.MenuDialogBuilder(this)
        .setCancelable(cancelAble).setCanceledOnTouchOutside(cancelAble)
        .setSkinManager(QMUISkinManager.defaultInstance(this))
        .setTitle(title)
        .addItems(listItems.toTypedArray()) { dialog, which ->
            callBack?.optionIndex(which)
            dialog.dismiss()
        }
        .create(R.style.DialogTheme3)
        .show()
}


fun DialogFragment.showOptionDialog(
    listItems: ArrayList<String>,
    callBack: OptionCallBack? = null
) {
    QMUIDialog.MenuDialogBuilder(activity)
        .setCancelable(true).setCanceledOnTouchOutside(true)
        .setSkinManager(QMUISkinManager.defaultInstance(activity))
        .addItems(listItems.toTypedArray()) { dialog, which ->
            callBack?.optionIndex(which)
            dialog.dismiss()
        }
        .create(R.style.DialogTheme3).show()
}

/***
 * 提示框
 */
fun AppCompatActivity.showTipsDialog(
    showDialogCondition: Boolean,
    title: String? = "提示",
    message: String,
    yes: () -> Unit
) {
    if (showDialogCondition) {
        val dialog = QMUIDialog.MessageDialogBuilder(this)
            .setTitle(title)
            .setMessage(message)
            .setSkinManager(QMUISkinManager.defaultInstance(this))
            .addAction(
                R.string.text_cancel
            ) { dialog, index -> dialog.dismiss() }
            .addAction(
                0,
                R.string.text_sure,
                QMUIDialogAction.ACTION_PROP_NEGATIVE
            ) { dialog, index ->
                dialog.dismiss()
                yes()
            }
            .create()
        dialog.show()
        dialog.setOnKeyListener { _, keyCode, _ ->
            if(keyCode==KeyEvent.KEYCODE_ENTER){
                dialog.dismiss()
                yes()
            }
            false
        }
    } else {
        yes()
    }


}

/***
 * 提示框
 */
fun AppCompatActivity.showTipsDialog(
    showDialogCondition: Boolean,
    title: String? = "提示",
    message: String,
    yes: () -> Unit,
    no: () -> Unit
) {
    if (showDialogCondition) {
        val dialog = QMUIDialog.MessageDialogBuilder(this)
            .setTitle(title)
            .setMessage(message)
            .setSkinManager(QMUISkinManager.defaultInstance(this))
            .addAction(
                R.string.text_cancel
            ) { dialog, index ->
                dialog.dismiss()
                no()
            }
            .addAction(
                0,
                R.string.text_sure,
                QMUIDialogAction.ACTION_PROP_NEGATIVE
            ) { dialog, index ->
                dialog.dismiss()
                yes()
            }
            .create()
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        dialog.show()
        dialog.setOnKeyListener { _, keyCode, _ ->
            if(keyCode==KeyEvent.KEYCODE_ENTER){
                dialog.dismiss()
                yes()
            }
            false
        }
    } else {
        yes()
    }


}

/***
 * 提示框
 */
fun AppCompatActivity.showTipsDialog(
    title: String? = "提示",
    message: String,
    yes: () -> Unit
) {

    val dialog = QMUIDialog.MessageDialogBuilder(this)
        .setTitle(title)
        .setMessage(message)
        .setSkinManager(QMUISkinManager.defaultInstance(this))
        .addAction(
            R.string.text_cancel
        ) { dialog, index -> dialog.dismiss() }
        .addAction(
            0,
            R.string.text_sure,
            QMUIDialogAction.ACTION_PROP_NEGATIVE
        ) { dialog, index ->
            dialog.dismiss()
            yes()
        }
        .create()
    dialog.show()
    dialog.setOnKeyListener { _, keyCode, _ ->
        if(keyCode==KeyEvent.KEYCODE_ENTER){
            dialog.dismiss()
            yes()
        }
        false
    }
}

fun Fragment.showTipsDialog(
    title: String? = "提示",
    message: String,
    yes: () -> Unit
) {

    val dialog = QMUIDialog.MessageDialogBuilder(requireContext())
        .setTitle(title)
        .setMessage(message)
        .setSkinManager(QMUISkinManager.defaultInstance(requireContext()))
        .addAction(
            R.string.text_cancel
        ) { dialog, index -> dialog.dismiss() }
        .addAction(
            0,
            R.string.text_sure,
            QMUIDialogAction.ACTION_PROP_NEGATIVE
        ) { dialog, index ->
            dialog.dismiss()
            yes()
        }
        .create()
    dialog.show()
    dialog.setOnKeyListener { _, keyCode, _ ->
        if(keyCode==KeyEvent.KEYCODE_ENTER){
            dialog.dismiss()
            yes()
        }
        false
    }
}

/**
 * 打开等待框
 */
fun AppCompatActivity.showTipDialogExt(message: String = "", tipCallBack: TipCallBack? = null) {
    if (!this.isFinishing) {
        if (tipDialogBuilder == null) {
            tipDialogBuilder =
                QMUITipDialog.Builder(this).setIconType(QMUITipDialog.Builder.ICON_TYPE_INFO)
                    .setTipWord(message)
                    .create()
            tipDialogBuilder?.setOnDismissListener {
                tipCallBack?.run { dismiss() }
            }
        }
        tipDialogBuilder?.show()
        dismissTipDialogExt()
    }
}


/**
 * 打开等待框
 */
fun Fragment.showTipDialogExt(message: String = "", tipCallBack: TipCallBack? = null) {
    activity?.let {
        if (!it.isFinishing) {
            if (tipDialogBuilder == null) {
                tipDialogBuilder =
                    QMUITipDialog.Builder(it).setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                        .setTipWord(message).create()
                tipDialogBuilder?.setOnDismissListener {
                    tipCallBack?.run { dismiss() }
                }
            }
            tipDialogBuilder?.show()
            dismissTipDialogExt()
        }
    }
}

/**
 * 关闭等待框
 */
fun AppCompatActivity.dismissTipDialogExt() {
    GlobalScope.launch(Dispatchers.Main) {
        delay(2000)
        tipDialogBuilder?.dismiss()
        tipDialogBuilder = null
    }

}

/**
 * 关闭等待框
 */
fun Fragment.dismissTipDialogExt() {
    dismissTipDialogExt()
}

fun vibrate() {
    val vib = MyApplication.instance.getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
    vib.vibrate(200)
}

fun vibrate(activity: Activity, milliseconds: Long) {
    val vib = activity.getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
    vib.vibrate(milliseconds)
}

fun vibrate(activity: Activity, pattern: LongArray?, isRepeat: Boolean) {
    val vib = activity.getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
    vib.vibrate(pattern, if (isRepeat) 1 else -1)
}

